﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购退货单命令实现
    /// </summary>
    public class AddPurchaseArrivalHandler : IRequestHandler<AddPurchaseArrivalCommand, APIResult<object>>
    {
        private readonly IBaseRepository<Preturnorder> _purchaseArrival;
        private readonly IMapper mapper;

        public AddPurchaseArrivalHandler(IBaseRepository<Preturnorder> purchaseArrival, IMapper mapper)
        {
            _purchaseArrival = purchaseArrival;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(AddPurchaseArrivalCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result=new APIResult<object>();
            result.Code = APIEnum.添加成功;
            result.Msg = APIEnum.添加成功.ToString();
            var entity = mapper.Map<Preturnorder>(request);
            entity.DocumentCode = "CGTH000001";
            //entity.DocumentTime = DateTime.Now;
            entity.DocumentState = "开立";
            entity.Caozuoyuan = "njrsun";
            entity.Shenheyuan = "njrsun";
            entity.ShenheDate = DateTime.Now;
            entity.Supid = 1;
            entity.Copynumber = 1;
            entity.ReturnsNum = "1";
            entity.OutinNum = "1";
            entity.CatchNum = "1";
            entity.material_code = 1;
            await _purchaseArrival.Add(entity);
            result.data = entity;
            return await Task.FromResult(result);
        }
    }
}
